package com.gitee.wsl.struct.command.model.parameters.options

import com.gitee.wsl.struct.command.internal.BadParameterValue
import com.gitee.wsl.struct.command.context.Context
import com.gitee.wsl.struct.command.model.parameters.TransformContext

/** A receiver for options transformers. */
class OptionCallTransformContext(
    /** The name that was used to invoke this option. */
    val name: String,
    /** The option that was invoked */
    val option: Option,
    /** The current command context */
    override val context: Context,
) : Option by option, TransformContext {
    override fun fail(message: String): Nothing = throw BadParameterValue(message, option, name)

    /** If [value] is false, call [fail] with the output of [lazyMessage] */
    inline fun require(value: Boolean, lazyMessage: () -> String = { "" }) {
        if (!value) fail(lazyMessage())
    }
}

/** A receiver for options transformers. */
class OptionTransformContext(
    /** The option that was invoked */
    val option: Option,
    /** The current command context */
    override val context: Context,
) : Option by option, TransformContext {
    /** Throw an exception indicating that usage was incorrect. */
    override fun fail(message: String): Nothing = throw BadParameterValue(message, option)

    /** If [value] is false, call [fail] with the output of [lazyMessage] */
    inline fun require(value: Boolean, lazyMessage: () -> String = { "" }) {
        if (!value) fail(lazyMessage())
    }
}
